package com.auth.Sercurity.demoList;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hujian
 * @version 1.0
 * @description: 观察者模式
 * @date 2022/2/16 17:41
 */
public class ObserverPattern  {

    public static void main(String[] args) {
        ConnreteSubject connreteSubject = new ConnreteSubject();
        Observer objectA = new ConnreteObjectA();
        Observer objectB = new ConnreteObjectB();
        connreteSubject.add(objectA);
        connreteSubject.add(objectB);
        connreteSubject.notifyObsever();
    }
    //抽象目标
   static abstract class Subject{
        protected List<Observer> observerList=new ArrayList();
        //增加观察者方法
        public  void add(Observer observer){
            observerList.add(observer);
        }
        //删除观察者方法
        public  void remove(Observer observer){
            observerList.remove(observer);
        }
    }

    //抽象观察者
    public  interface Observer{
        void response();
    }
    //具体目标
    public static class ConnreteSubject extends Subject{
        public   void notifyObsever(){
            System.out.println("具体目标发生改变");
            System.out.println("----------------");
            for (Observer observer : observerList) {
                observer.response();
            }
        }
        
    }
    //具体观察者A
    public static class ConnreteObjectA implements Observer{

        @Override
        public void response() {
            System.out.println("具体观察者A作出反应");
        }
    }
    //具体观察者B
    public static class ConnreteObjectB implements Observer{

        @Override
        public void response() {
            System.out.println("具体观察者B作出反应");
        }
    }
}
